package cz.zcu.fav.ratdroid.xml;

import org.joda.time.LocalDate;
import org.simpleframework.xml.convert.Converter;
import org.simpleframework.xml.stream.InputNode;
import org.simpleframework.xml.stream.OutputNode;

import cz.zcu.fav.ratdroid.bean.Occupation;

public class OccupationConverter implements Converter<Occupation>
{
	private static String OCCUPATION_DATE = "date";
	private static String OCCUPATION_COUNT = "count";

	public Occupation read(InputNode node) throws Exception
	{
		Occupation occupation = new Occupation();
		InputNode child;
		while ((child = node.getNext()) != null)
		{
			fillOccupation(occupation, child);
		}
		return occupation;
	}

	private void fillOccupation(Occupation occupation, InputNode child) throws Exception
	{
		String value = child.getValue();
		if (child.getName().equals(OCCUPATION_DATE))
		{
			occupation.setDate(new LocalDate(value));
		} else if (child.getName().equals(OCCUPATION_COUNT))
		{
			occupation.setCount(Integer.parseInt(value));
		}
	}

	public void write(OutputNode node, Occupation value) throws Exception
	{
		throw new UnsupportedOperationException();
	}

}
